SUBROUTINE WRITE_ITERDAT(it)
!
! Verwendungszweck:
!     Ausgabe der Werte der primaeren Variablen inklusive einem
!     Header mit den allerwichtigsten Daten und den Randbedingungen
!
!-----------------------------------------------------------------------
! Interne Variable:
!
!   idx          : Schleifenindex
!   modnrchar    : Die Modellnummer "modnr" als CHARACTER -Variable
!   modnrcharlen : Die Laenge von "modchar"
!   itchar       : Der Iterationschritt "it" als CHARACTER -Variable
!   itcharlen    : Die Laenge von "itchar"
!   datfilelen   : Die Laenge von "datfile"
!   filename     : Name der Output-Files
!   filenamelen  : Die Laenge von "filename"
!   archive_reference : Name des zum Kompilat gehoerigen Archiv zip-files
!   backup_reference  : Name des letzten vollstaendigen Backups
!
      use config,  only : np
      use global,  only : t, datfile, logfilename, modnr
      use primvar, only : nv, X, MR, MD, ME, MUr, MUp, MJ, MH
      use RBvar,   only : M_star, L_star, R_in, rho_in, e_in, R_out, rho_out, e_out
      use zvar,    only : pseudo2D


      implicit none

      integer                       :: it, idx, datfilelen, itcharlen, modnrcharlen, filenamelen
      character(len=9)              :: itchar, modnrchar
      character(len=31)             :: archive_reference
      character(len=29)             :: backup_reference
      character(len=124)            :: filename   ! 2 mal 50 +".???????.iter???????.dat"

!-----------------------------------------------------------------------
!    Zusammensetzen des File-Namens
!-----------------------------------------------------------------------

      write(itchar,"(I9)") it

      itchar = adjustl( itchar )
      itcharlen = len_trim( itchar )

      write(modnrchar,"(I9)") modnr

      modnrchar = adjustl( modnrchar )
      modnrcharlen = len_trim( modnrchar )

      datfilelen = len_trim ( datfile )

      filename  = datfile(1:datfilelen) // "." // modnrchar(1:modnrcharlen) // ".iter" // itchar(1:itcharlen) // ".dat"
      filenamelen = datfilelen + 1 + modnrcharlen + 5 + itcharlen + 4

      include '../../common-code/archive-reference.txt'
      include '../../common-code/backup-reference.txt'


!-----------------------------------------------------------------------
!    Ausgabe
!-----------------------------------------------------------------------

      open(unit=10,file=filename,action="write",status="new",err=999)

         write(10,"(a)")                             "# 1DD model file"
         write(10,"(a,a)")                           "#    corresponding log file     : ", logfilename
         write(10,"(a,a)")                           "#    corresponding code archive : ", archive_reference
         write(10,"(a,a)")                           "#    last full backup           : ", backup_reference
         write(10,"(a)")                             "#"
         write(10,"(a)")                             "# np, nv, pseudo2D, t, M_star, L_star, R_in, rho_in, e_in, R_out, rho_out, e_out"
         write(10,"(a,2(1xi8),1xL1,9(1xES23.15E3))") "#",np, nv, pseudo2D, t, M_star, L_star, R_in, rho_in, e_in, R_out, rho_out, e_out
         write(10,"(a)")                             "#"
         write(10,"(a)")                             "# data format:   1   2   3   4   5    6    7   8"
         write(10,"(a)")                             "#               idx, MR, MD, ME, MUr, MUp, MJ, MH"
         do idx = 1,np
            write(10,"(i8,7(1xES23.15E3))") &
            idx, X(MR,idx), X(MD,idx) , X(ME,idx), X(MUr,idx), X(MUp,idx), X(MJ,idx) , X(MH,idx)
         end do

      close(unit=10)

      write(66,"(a,a,a)")"write_iterdat.f90:   Datei ",filename(1:filenamelen)," geschrieben"

      RETURN

999   continue

      write(66,"(a,a,a)")"write_iterdat.f90:   Fehler beim Anlegen des Datenfiles: ",filename(1:filenamelen)," -> output uebersprungen ! "

END SUBROUTINE WRITE_ITERDAT
